package com.leadingsoft.pwxk.controller;

import java.time.ZonedDateTime;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.leadingsoft.common.dto.TableResultDto;
import com.leadingsoft.pwxk.converter.OperationLogConverter;
import com.leadingsoft.pwxk.dto.OperationLogDto;
import com.leadingsoft.pwxk.service.LogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author ysh
 * @desc 操作日志
 */
@RequestMapping("api/logs")
@RestController
@Api(description = "日志相关")
public class LogController {

	@Autowired
	LogService logService;

	@Autowired
	OperationLogConverter operationLogConverter;

	@GetMapping(params = { "draw" })
	@ApiOperation("列表")
	public TableResultDto<OperationLogDto> getList(
			@PageableDefault(page = 0, size = 10, sort = { "date" }, direction = Direction.DESC) Pageable pageable,
			@RequestParam(value = "draw", required = false) Long draw,
			@RequestParam(value = "search", required = false) String search,
			@DateTimeFormat(iso = ISO.DATE_TIME) @RequestParam(value = "start", required = false) ZonedDateTime start,
			@DateTimeFormat(iso = ISO.DATE_TIME) @RequestParam(value = "end", required = false) ZonedDateTime end) {
		return TableResultDto.success(draw, logService.getList(pageable, search, start, end), operationLogConverter);
	}

	@GetMapping("/loginLog")
	@ApiOperation("登入日志导出")
	public void exportLoginLog(HttpServletResponse response,
			@RequestParam(value = "search", required = false) String search,
			@DateTimeFormat(iso = ISO.DATE_TIME) @RequestParam(value = "start", required = false) ZonedDateTime start,
			@DateTimeFormat(iso = ISO.DATE_TIME) @RequestParam(value = "end", required = false) ZonedDateTime end,
			@RequestParam(value = "title", required = false, defaultValue = "登录日志") String title) {
		logService.exportLoginLog(search, start, end, response, title);
	}

	@GetMapping("/OperationLog")
	@ApiOperation("操作日志导出")
	public void exportOperationLog(HttpServletResponse response,
			@RequestParam(value = "search", required = false) String search,
			@DateTimeFormat(iso = ISO.DATE_TIME) @RequestParam(value = "start", required = false) ZonedDateTime start,
			@DateTimeFormat(iso = ISO.DATE_TIME) @RequestParam(value = "end", required = false) ZonedDateTime end,
			@RequestParam(value = "title", required = false, defaultValue = "操作日志") String title) {
		logService.exportOperationLog(search, start, end, response, title);
	}

}
